﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchjifeng.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");
define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',10);
define('ONCECOMMENTCATCHCOUNT',10);

define('WEBURLHEADER','http://apk.gfan.com/Product/App');
define('WEBURLFOOTER','.html');




//http://3g.gfan.com/data/index.php?/lists/ajaxpage/1/3/app/3/?callback=jQuery16105541677612345666_1373352697619&_=1373352755035
//http://3g.gfan.com/data/index.php?/lists/ajaxpage/1/1/app/3/?callback=jQuery161008269634004682302_1373352805380&_=1373352805449
define('LOGFORSUCCESS',0);
define('WAPURLHEADERGFAN','http://3g.gfan.com/data/index.php?/detail/ajaxdetail/');

getinfoandrank();

//getcomment();


function getinfoandrank()
{
	
//	decodeurlandgetdataweb(101,"soft","http://apk.gfan.com/apps_7_1_");
	
	decodeurlandgetdataweb(102,"game","http://apk.gfan.com/games_8_1_");

	catchother();
}





function decodeurlandgetdataweb($catchtype,$appbigtype,$urlheader)
{
	$app_exit_versions = get_exit_appversion_fromdb();
	$app_today_data = get_today_data();
    $app_exit_arr = get_exit_packagenameandid_fromdb();
	for($i=1;;)
	{
	    $apps_url_app_all_list = $urlheader.$i.".html";
	    //echo $apps_url_app_all_list;die();
	    $appidarr = getappidarrfromlist($apps_url_app_all_list);
	    
	    if((count($appidarr) == 0) || ($appidarr == ""))
	    {
	    	break;
	    }
	    
	    $rankpos = ($i-1) * 30;
	    $app_info_arr_insertdb_data = array();
	    $appdetail_info_insertdb_arr = array();
	    foreach ($appidarr as $appid)
	    {
	    	$rankpos++;

//	    	if(in_array($packagename,$app_exit_packagename))
	    	{
				$app_info_arr_insertdb['app_id'] = 0;
				$app_info_arr_insertdb['app_version_id'] = 0;
				
				foreach ($app_exit_arr as $app_exit)
				{
					if($appid == $app_exit["app_id"])
					{
						$app_info_arr_insertdb['app_id'] = $app_exit['id'];
						break;
					}
				}
				if($app_info_arr_insertdb['app_id'] != 0)
				{
					foreach ($app_exit_versions as $app_exit_version)
					{
						if($app_info_arr_insertdb['app_id'] == $app_exit_version['app_id'])
						{
							$app_info_arr_insertdb['app_version_id'] = $app_exit_version['id'];
						}
					}
				}
				if($app_info_arr_insertdb['app_id'] != 0)
				{
					$app_info_arr_insertdb['AppRanking'] = $rankpos;
					$app_info_arr_insertdb['AppRankingType'] = $catchtype;
					if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
					{
						$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
					}
				}
					
				
	    	}
//	    	else
//	    	{
//	    		$wapurl_footer = ".html";
//				$wapurl = WAPURLHEADERWANDOUJIA.$packagename.WAPURLFOOTERWANDOUJIA;
//				$appdetail_info_arr = getappdetailinfowandoujia($packagename,$wapurl,$catchtype,$appbigtype);
//				$appdetail_info_arr['AppRanking'] = $rankpos;
//				
//				$appdetail_info_insertdb_arr[] = $appdetail_info_arr;
//	    	}
	    }
	    
	    update_apprankpc_todb($app_info_arr_insertdb_data);
//	    insertintodbpc($appdetail_info_insertdb_arr);
	    $i++;
	}
}


function get_exit_packagenameandid_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, packageName,title,app_id from jk_apps_info_jifeng where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_arr = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_arr_temp['id'] = $row['id'];
	  	$app_exit_arr_temp['packageName'] = $row['packageName'];
	  	$app_exit_arr_temp['app_id'] = $row['app_id'];
	  	$app_exit_arr[] = $app_exit_arr_temp;
  	}
	return $app_exit_arr;
}


//排名信息
function update_apprankpc_todb($app_info_arr)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	if(count($app_info_arr) == 0)
	{
		return ;
	}
	
	$sql = 'insert into jk_apps_data_web_jifeng_'.date('Ym',time()).' (app_id,app_version_id,rank,ranktype,time) values '; 

	$i=0;
	foreach ($app_info_arr as $app_info)
	{
		if($i==0)
		{
			$sql .= '('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		else
		{
			$sql .= ',('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		$i++;
	}
	
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/jifeng_success.log';
	$filename_fail = LOGPATHHEADER.'logs/jifeng_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/jifeng_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}


function getappidarrfromlist($urllist)
{
	$app_id_arr = array();
	//$ranking = $flag ;//* ONCECATCHCOUNT;
	
	// 新建一个Dom实例 
	$html = new simple_html_dom();
	// 从url中加载
	@$html = file_get_html($urllist);
	if($html == "")
	{
		@$html = file_get_html($urllist);
		if($html == "")
		{
			return $app_id_arr;
		}
	}

	$appinfoarr = $html->find('div[id=container] div.clearfix div[id=content] ul.appList li.clearfix ');
	foreach ($appinfoarr as $appinfotemp)
	{
		$appid = $appinfotemp->find('a',0)->href;
		$appid =  str_replace("/Product/App","",$appid);
		$appid =  str_replace(".html","",$appid);
		$app_id_arr[] = $appid;
	}
	$html->clear();
	return $app_id_arr;
}




function get_exit_appversion_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id,versionName,versionCode from jk_apps_version_jifeng where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_version = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_version_temp = array();
	  	$app_exit_version_temp['id'] = $row['id'];
	  	$app_exit_version_temp['app_id'] = $row['app_id'];
	  	$app_exit_version_temp['versionName'] = $row['versionName'];
	  	$app_exit_version_temp['versionCode'] = $row['versionCode'];
	  	$app_exit_version[] = $app_exit_version_temp;
  	}
	return $app_exit_version;
}

function get_today_data()
{
	$today = date("Y-m-d");
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = 'select id, app_id,rank,ranktype,time from jk_apps_data_web_jifeng_'.date('Ym',time()).' where IsDeleted=0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
  		$app_exit_info_temp['rank'] = $row['rank'];
	  	$app_exit_info_temp['ranktype'] = $row['ranktype'];
	  	$app_exit_info_temp['time'] = $row['time'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}


function searchisexittodaydata($app_infos, $app_today_data) 
{
	foreach ($app_today_data as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['AppRanking'] == $app_exit["rank"]) && ($app_infos['AppRankingType'] == $app_exit["ranktype"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}


?>